#include "mainscene.h"
#include "config.h"
#include <QIcon>
#include <QPainter>
#include <QMouseEvent>
#include <ctime>
#include<QPushButton>
#include<QMainWindow>
#include<QTextEdit>
#include<QLabel>
#include<QWidget>
#include<QMessageBox>
#include<QDebug>

MainScene::MainScene(QWidget *parent)
    : QWidget(parent)
{
    //初始化窗口大小
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);

    //设置窗口标题
    setWindowTitle(GAME_TITLE);

    //设置窗口标题
    setWindowIcon(QIcon(GAME_ICON));

    //设置登录按钮
    QPushButton* btn1=new QPushButton;
    btn1->setParent(this);
    btn1->resize(200,100);
    btn1->setText("开始游戏");
    btn1->setIcon(QIcon(BUTTON_ICON));
    btn1->setIconSize(QSize(100,100));
    btn1->move(156,200);



    //设置游戏标题
    QLabel* lab=new QLabel;
    lab->setParent(this);
    lab->setText(TITLE);
    lab->move(120,100);
    QFont ft;
    ft.setPointSize(30);
    lab->setFont(ft);
    QPalette pa;
    pa.setColor(QPalette::WindowText,Qt::cyan);
    lab->setPalette(pa);



    //构建介绍窗口
    QPushButton* btn2=new QPushButton;
    btn2->setParent(this);
    btn2->resize(200,100);
    btn2->setText("了解游戏");
    btn2->setIcon(QIcon(BUTTON_ICON));
    btn2->setIconSize(QSize(100,100));
    btn2->move(156,500);
    QMainWindow* introduction=new QMainWindow;
    introduction->setFixedSize(500,500);
    introduction->setWindowTitle(INTRODUCTION_TITLE);
    introduction->setWindowIcon(QIcon(GAME_ICON));
    QTextEdit * edit = new QTextEdit(introduction);
    introduction->setCentralWidget(edit);
    edit->append(INTRODUCTION_TITLE);
    edit->append(INTRODUCTION);
    edit->append(INTRODUCTION2_TITLE);
    edit->append(INTRODUCTION1);
    edit->append(INTRODUCTION2);
    connect(btn2,&QPushButton::clicked,introduction,&QMainWindow::show);



    //构建商店窗口
    QPushButton* btn3=new QPushButton;
    btn3->setParent(this);
    btn3->resize(200,100);
    btn3->setText("选择球员");
    btn3->setIcon(QIcon(BUTTON_ICON));
    btn3->setIconSize(QSize(100,100));
    btn3->move(156,400);
    QMainWindow* shop=new QMainWindow;
    shop->setFixedSize(500,500);
    shop->setWindowTitle(SHOP_TITLE);
    shop->setWindowIcon(QIcon(GAME_ICON));
    connect(btn3,&QPushButton::clicked,shop,&QMainWindow::show);

    //设置选择功能按钮
    QPushButton* shopbtn1=new QPushButton;
    shopbtn1->setParent(shop);
    shopbtn1->resize(200,100);
    shopbtn1->setText("使用球员1");
    shopbtn1->setIcon(QIcon(BUTTON_ICON));
    shopbtn1->setIconSize(QSize(100,100));
    shopbtn1->move(156,100);
    connect(shopbtn1,&QPushButton::clicked,[=](){
        m_hero.heroset=1;
        m_hero.update();
        update();
    });
    QPushButton* shopbtn2=new QPushButton;
    shopbtn2->setParent(shop);
    shopbtn2->resize(200,100);
    shopbtn2->setText("使用球员2");
    shopbtn2->setIcon(QIcon(BUTTON_ICON));
    shopbtn2->setIconSize(QSize(100,100));
    shopbtn2->move(156,200);
    connect(shopbtn2,&QPushButton::clicked,[=](){
        m_hero.heroset=2;
        m_hero.update();
        update();
    });
    QPushButton* shopbtn3=new QPushButton;
    shopbtn3->setParent(shop);
    shopbtn3->resize(200,100);
    shopbtn3->setText("使用球员3");
    shopbtn3->setIcon(QIcon(BUTTON_ICON));
    shopbtn3->setIconSize(QSize(100,100));
    shopbtn3->move(156,300);
    connect(shopbtn3,&QPushButton::clicked,[=](){
        m_hero.heroset=3;
        m_hero.update();
        update();
    });


    //构建选关窗口
    QPushButton* btn4=new QPushButton;
    btn4->setParent(this);
    btn4->resize(200,100);
    btn4->setText("选择关卡");
    btn4->setIcon(QIcon(BUTTON_ICON));
    btn4->setIconSize(QSize(100,100));
    btn4->move(156,300);
    QMainWindow* choose=new QMainWindow;
    choose->setFixedSize(500,500);
    choose->setWindowTitle(CHOOSE_TITLE);
    choose->setWindowIcon(QIcon(GAME_ICON));
    connect(btn4,&QPushButton::clicked,choose,&QMainWindow::show);

    //设置关卡选择按钮
    QPushButton* choosebtn1=new QPushButton;
    choosebtn1->setParent(choose);
    choosebtn1->resize(200,100);
    choosebtn1->setText("选择关卡1");
    choosebtn1->setIcon(QIcon(BUTTON_ICON));
    choosebtn1->setIconSize(QSize(100,100));
    choosebtn1->move(156,100);
    connect(choosebtn1,&QPushButton::clicked,[=](){
        m_map.mapset=1;
        m_map.update();
        update();
    });
    QPushButton* choosebtn2=new QPushButton;
    choosebtn2->setParent(choose);
    choosebtn2->resize(200,100);
    choosebtn2->setText("选择关卡2");
    choosebtn2->setIcon(QIcon(BUTTON_ICON));
    choosebtn2->setIconSize(QSize(100,100));
    choosebtn2->move(156,300);
    connect(choosebtn2,&QPushButton::clicked,[=](){
        m_map.mapset=2;
        m_map.update();
        update();
    });
    //点击开始游戏按钮，进入游戏
    connect(btn1,&QPushButton::clicked,[=](){

        //调用初始化场景函数
        initScene();

        btn1->hide();
        lab->hide();
        btn2->hide();
        btn3->hide();
        btn4->hide();
    });

}

MainScene::~MainScene()
{

}

void MainScene::initScene()
{

    //设置定时器间隔
    m_Timer.setInterval(GAME_RATE);

    //调用启动游戏接口
    playGame();

    //敌人出场纪录变量 初始化
    m_recorder = 0;


    //随机数种子
    srand((unsigned int)time(NULL));  //头文件  #include <ctime>
}

void MainScene::playGame()
{

    //玩游戏 启动定时器
    m_Timer.start();

    //监听定时器的信号
    connect(&m_Timer , &QTimer::timeout,[=](){
        //敌人出场
        enemyToScene();
        //更新游戏中所有元素的坐标
        updatePosition();
        //游戏中的元素 绘制到屏幕中
        update(); //再调用paintEvent函数

        //碰撞检测
        collisionDetection();

        //达阵检测
        TouchDown();
    });
}

void MainScene::updatePosition()
{
    //更新地图坐标
    m_map.mapPosition();

    //投球
    m_hero.shoot();

    //计算球坐标
    for(int i = 0 ;i < BULLET_NUM;i++)
    {
        //如果球状态为非空闲，计算发射位置
        if(m_hero.m_bullets[i].m_Free == false)
        {
            m_hero.m_bullets[i].updatePosition();
        }
        if(m_hero.m_bullets2[i].m_Free == false)
        {
            m_hero.m_bullets2[i].updatePosition();
        }
        if(m_hero.m_bullets3[i].m_Free == false)
        {
            m_hero.m_bullets3[i].updatePosition();
        }

    }

    //敌人坐标计算
    for(int i = 0 ; i< ENEMY_NUM;i++)
    {
        //非空闲敌人 更新坐标
       if(m_enemys[i].m_Free == false)
       {
          m_enemys[i].updatePosition();
       }
    }

    //计算爆炸播放的图片
    for(int i = 0 ; i < BOMB_NUM;i++)
    {
        if(m_bombs[i].m_Free == false)
        {
           m_bombs[i].updateInfo();
        }
    }

}

void MainScene::paintEvent(QPaintEvent *event)
{
    //利用画家画图图片
    QPainter painter(this);

    //绘制地图
    painter.drawPixmap(0,m_map.m_map1_posY,m_map.m_map1);
    painter.drawPixmap(0,m_map.m_map2_posY,m_map.m_map2);

    //绘制我方
    painter.drawPixmap(m_hero.m_X,m_hero.m_Y,m_hero.m_Plane);


    //绘制橄榄球
    for(int i = 0 ;i < BULLET_NUM;i++)
    {
        //如果橄榄球状态为非空闲 ，绘制图片
        if(m_hero.m_bullets[i].m_Free == false)
        {
            painter.drawPixmap(m_hero.m_bullets[i].m_X,m_hero.m_bullets[i].m_Y,m_hero.m_bullets[i].m_Bullet);
        }
        if(m_hero.m_bullets2[i].m_Free == false)
        {
            painter.drawPixmap(m_hero.m_bullets2[i].m_X,m_hero.m_bullets2[i].m_Y,m_hero.m_bullets2[i].m_Bullet);
        }
        if(m_hero.m_bullets3[i].m_Free == false)
        {
            painter.drawPixmap(m_hero.m_bullets3[i].m_X,m_hero.m_bullets3[i].m_Y,m_hero.m_bullets3[i].m_Bullet);
        }

    }

    //绘制敌人
    for(int i = 0 ; i< ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free == false)
        {
            painter.drawPixmap(m_enemys[i].m_X,m_enemys[i].m_Y,m_enemys[i].m_enemy);
        }
    }

    //绘制爆炸图片
    for(int i = 0 ; i < BOMB_NUM;i++)
    {
        if(m_bombs[i].m_Free == false)
        {
           painter.drawPixmap(m_bombs[i].m_X,m_bombs[i].m_Y,m_bombs[i].m_pixArr[m_bombs[i].m_index]);
        }
    }

    //结束游戏
    if(m_hero.blood==0){
        m_Timer.stop();
        QString temp;
        temp=tr("%1").arg(score);
        QString tempstr="您的得分为"+temp+",是否重新开始游戏?";
        int click=QMessageBox::question(this,"游戏结束",tempstr);
        if(click==QMessageBox::Yes){
            m_Timer.start();
            m_hero.update();
            update();
        }
        else{
            this->close();
        }
        m_hero.blood=5;
        score=0;
        }
}

void MainScene::mouseMoveEvent(QMouseEvent * event)
{
    int x =  event->x() -m_hero.m_Rect.width() * 0.5;
    int y =  event->y() -m_hero.m_Rect.height() *0.5;

    //边界检测

    if(x <= 0 )
    {
        x = 0;
    }
    if(x >= GAME_WIDTH - m_hero.m_Rect.width())
    {
        x = GAME_WIDTH - m_hero.m_Rect.width();
    }
    if(y <= 0)
    {
        y = 0;
    }
    if(y >= GAME_HEIGHT - m_hero.m_Rect.height())
    {
        y = GAME_HEIGHT - m_hero.m_Rect.height();
    }

    m_hero.setPosition(x,y);
}

void MainScene::enemyToScene()
{
    //累加出场间隔
    m_recorder++;
    if(m_recorder < ENEMY_INTERVAL)
    {
        return;
    }

    m_recorder = 0;

    for(int i = 0 ; i< ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free)
        {
            //敌人空闲状态改为false
            m_enemys[i].m_Free = false;
            //设置坐标
            m_enemys[i].m_X = rand() % (GAME_WIDTH - m_enemys[i].m_Rect.width());
            m_enemys[i].m_Y = -m_enemys[i].m_Rect.height();
            break;
        }
    }
}

void MainScene::collisionDetection()
{
    //遍历所有非空闲的敌人
    for(int i = 0 ;i < ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free)
        {
            //空闲 跳转下一次循环
            continue;
        }
        //检测是否与我方碰撞
         if(m_enemys[i].m_Rect.intersects(m_hero.m_Rect)){
             m_hero.blood--;
             //播放音效
             QSound::play(SOUND_BOMB);

             m_enemys[i].m_Free = true;
         }
        //遍历所有 非空闲的球
        for(int j = 0 ; j < BULLET_NUM;j++)
        {
            if(m_hero.m_bullets[j].m_Free)
            {
                //空闲橄榄球，跳转下一次循环
                continue;
            }

            //如果球矩形框和敌人矩形框相交，发生碰撞，同时变为空闲状态即可
            if(m_enemys[i].m_Rect.intersects(m_hero.m_bullets[j].m_Rect))
            {
                //得分增加
                score++;
                //播放音效
                QSound::play(SOUND_BOMB);


                m_enemys[i].m_Free = true;
                m_hero.m_bullets[j].m_Free = true;

                //调用爆炸特效
                //播放爆炸效果
              for(int k = 0 ; k < BOMB_NUM;k++)
              {
                  if(m_bombs[k].m_Free)
                  {
                      //爆炸状态设置为非空闲
                      m_bombs[k].m_Free = false;
                      //更新坐标
                      m_bombs[k].m_X = m_enemys[i].m_X;
                      m_bombs[k].m_Y = m_enemys[i].m_Y;
                      break;
                  }
              }

            }
        }
    }
}
void MainScene::TouchDown(){
    if(m_map.mapset==2){
        for(int i = 0 ; i< ENEMY_NUM;i++)
        {
            //敌人达阵检测
            if(m_enemys[i].m_Free==false){
                if(m_enemys[i].m_Y>=GAME_HEIGHT-m_enemys[i].m_Rect.height()){
                    m_enemys[i].m_Free=true;
                    m_hero.blood--;
                }
            }
        }
    }
}
